/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package control.khachhang;

import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import model.dao.khachhang.KhachHangDAO;
import model.entity.KhachHang;

/**
 *
 * @author Admin
 */
public class KHSuaThongTin_Full_Ctrl {
    
    /**
     * Liệt kê theo tên KH
     */
    public static final void lietKeTheoTenKhachHang(JTextField tfKH, JTable tbl){
        String tenKH = tfKH.getText();
        List list = KhachHangDAO.lietKeTheoTenKhachHang(tenKH);
        Vector<String>tableHeader = new Vector<String>();
        tableHeader.add("Mã KH");
        tableHeader.add("Họ");
        tableHeader.add("Tên");
        tableHeader.add("Giới Tính");
        tableHeader.add("Ngày Sinh");
        tableHeader.add("Địa Chỉ");
        tableHeader.add("SĐT");
        tableHeader.add("Ghi Chú");
        
        Vector data = new Vector();
        for (Object object : list) {
            KhachHang kh = (KhachHang)object;
            Vector<Object>oneRow = new Vector<Object>();
            oneRow.add(kh.getKhMa());
            oneRow.add(kh.getKhHo());
            oneRow.add(kh.getKhTen());
            oneRow.add(kh.getKhGioiTinh());
            oneRow.add(kh.getKhNgaySinh());
            oneRow.add(kh.getKhDiaChi());
            oneRow.add(kh.getKhSdt());
            oneRow.add(kh.getKhGhiChu());
            
            data.add(oneRow);
            
        }
        tbl.setModel(new DefaultTableModel(data,tableHeader));
    }
    
    public static final void lietKeTheoMaKhachHang(JTextField tfMaKH, JTable tbl){
        int maKH = Integer.parseInt(tfMaKH.getText());
        List list = KhachHangDAO.lietKeTheoMaKhachHang(maKH);
        Vector<String>tableHeader = new Vector<String>();
        tableHeader.add("Mã KH");
        tableHeader.add("Họ");
        tableHeader.add("Tên");
        tableHeader.add("Giới Tính");
        tableHeader.add("Ngày Sinh");
        tableHeader.add("Địa Chỉ");
        tableHeader.add("SĐT");
        tableHeader.add("Ghi Chú");
        
        Vector data = new Vector();
        for (Object object : list) {
            KhachHang kh = (KhachHang)object;
            Vector<Object>oneRow = new Vector<Object>();
            oneRow.add(kh.getKhMa());
            oneRow.add(kh.getKhHo());
            oneRow.add(kh.getKhTen());
            oneRow.add(kh.getKhGioiTinh());
            oneRow.add(kh.getKhNgaySinh());
            oneRow.add(kh.getKhDiaChi());
            oneRow.add(kh.getKhSdt());
            oneRow.add(kh.getKhGhiChu());
            
            data.add(oneRow);
            
        }
        tbl.setModel(new DefaultTableModel(data,tableHeader));
    }
    
    /**
     * 
     */
    public static KhachHang selectRow(JTable table){
        int row = table.getSelectedRow();
        int maKH = (Integer) table.getValueAt(row, 0);
        String hoKH = (String)table.getValueAt(row, 1);
        String tenKH = (String)table.getValueAt(row, 2);
        String gioiTinhKH = (String)table.getValueAt(row, 3);
        Date ngaySinhKH = (Date) table.getValueAt(row, 4);
        String diaChiKH = (String)table.getValueAt(row, 5);
        String sDTKH = (String)table.getValueAt(row, 6);
        String ghiChuKH = (String)table.getValueAt(row, 7);
        
        KhachHang kh = new KhachHang();
        kh.setKhMa(maKH);
        kh.setKhHo(hoKH);
        kh.setKhTen(tenKH);
        kh.setKhGioiTinh(gioiTinhKH);
        kh.setKhDiaChi(diaChiKH);
        kh.setKhGhiChu(ghiChuKH);
        kh.setKhNgaySinh(ngaySinhKH);
        kh.setKhSdt(sDTKH);
        return kh;
        
    }
}
